
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: "1",
        fileList: [],//文件列表
        videoFileList:[],//视频文件列表
        findVrType:"1",//默认为图片资源
        options: {},
        show: false,//是否显示场景弹框
        titleVrList: [],//场景命名
        fileTitle: "",//当前图片场景名称
        infoId:"",//对应的vr_info表的ID
        currentItem:{
            index:undefined,
            name:undefined
        }
    },
    handleTabChange(event) {
        const { value } = event.currentTarget.dataset;
        this.setData({
          active: value,
          fileTitle:[],
          findVrType:value
        });
        //加载数据
        if('1' === value) {
            //加载图片资源
            this.findVrInfoDetailByInfoId();
            
        } else if('2' === value) {
             //加载视频资源
             this.findVrInfoDetailByInfoId();
        }else if('3' === value) {
            //加载VR资源
            this.findVrInfoDetailByInfoId();
        }

        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            options
        })
        this.setData({
            infoId:options.infoId
        })
        
        this.findVrInfoDetailByInfoId()
        this.onFindVRTitleList()
    },
    changeFiles(e) {
        const files = e.detail;
        this.setData({
            fileList: files
        })
    },
    async findVrInfoDetailByInfoId() {
        let {
            options
        } = this.data
        //console.log("----------x"+options.infoId);
        const res = await wx.ajax.get('findVrInfoDetailByInfoId.action', {
            id: options.infoId || '',
            type:this.data.findVrType
        }, true)
        let files = res.result.map(item => ({
            ...item,
            url: item.file_id,
            name: item.title
        }))        
        this.setData({
            fileList: files
        })
        //console.log("fileList------"+JSON.stringify(this.data.fileList))
    },

    editTile(e) {
        let {index}=e.detail
        this.setData({
            show: true,
            currentItem:{
                ...this.data.currentItem,
                index
            }
        })
    },
    clickItemTitle(e){
        const {name}= e.currentTarget.dataset
        this.setData({
            currentItem:{
                ...this.data.currentItem,
                name
            }
        })
        this.setData({
            fileTitle:name
        })
    },
    onClickTitle(){
        const {name,index}=this.data.currentItem
        console.log(name)
        this.data.fileList[index].name=this.data.fileTitle;
        this.setData({
            show:false,
            fileList:this.data.fileList
        })

    },
    async onFindVRTitleList() {
        let {
            result
        } = await wx.ajax.get('findVrType.action', {
            type: 'cjm'
        })
        // console.log(result);
        this.setData({
            titleVrList: result
        });
    },

    onClose() {
        this.setData({
            show: false
        });
    },

    //保存 数据
    async submitSave() {

        let {
            fileList,
            options
        } = this.data
        if (fileList.length === 0) {
            wx.showToast({
                title: '请上传后在提交',
                icon: 'none'
            })
            return
        }
        let data = {
            title: (this.formatData(fileList, 'name')).toString(),
            info_id: options.infoId || '',
            type: this.data.findVrType,
            file_id: (this.formatData(fileList, 'url')).toString()
        }
        //console.log("infoId--data---------"+JSON.stringify(data))
        const res = await wx.ajax.post('addVrInfoDetail.action', data, true)
        wx.showToast({
            title: res.msg,
            icon: 'none'
        })
    },
    formatData(arr, key) {
        return arr.map(item => item[key])
    },
    deleteHandle(event) {
        const { index } = event.detail;
    
        const { videoFileList} = this.data;
        videoFileList.splice(index, 1);
        this.setData({
            videoFileList
        });
      },
      afterReadHandle(event) {
        const file = event.detail.file;
    
        const { videoFileList} = this.data;
    
        wx.showLoading({
          title: "上传中...",
        });
        wx.ajax.uploadFile({
          filePath: file.url,
          callback: (url) => {
            wx.hideLoading();
            if (url) {
                videoFileList.push(file);
              this.setData({
                videoFileList
              });
            }
          },
        });
      },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})